package com.sxj.corejava.code02_jdbc;

import java.sql.*;

/**
 * @author 石小俊
 * @date 2023年12月2023/12/2日 14:50
 */
public class Test10_批处理 {

    public static void main(String[] args) throws SQLException {
        test01();
        test02();
    }

    public static void test01() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8", "root", "");


            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_user ")
                    .append("   (username,password,phone,address)")
                    .append(" values ")
                    .append("   (?,?,?,?)")
                    .toString();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                ps = conn.prepareStatement(sql);
                ps.setString(1, "abc");
                ps.setString(2, "123456");
                ps.setString(3, "13812345678");
                ps.setString(4, "江苏-南京");
                ps.executeUpdate();
            }
            long end = System.currentTimeMillis();
            System.out.println("添加10000条数据共耗费了" + (end - begin) + "毫秒");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("未找到驱动");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库失败");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器内部异常");
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("释放连接出错");
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void test02() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8","root","");
            // 开启事务
            conn.setAutoCommit(false);

            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_user ")
                    .append("   (username,password,phone,address)")
                    .append(" values ")
                    .append("   (?,?,?,?)")
                    .toString();
            long begin = System.currentTimeMillis();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < 10000; i++) {
                ps.setString(1, "abc");
                ps.setString(2, "123456");
                ps.setString(3, "13812345678");
                ps.setString(4, "江苏-南京");
                // 将状态集对象添加到批处理的缓冲区,此时不执行添加操作
                ps.addBatch();
            }
            // 批量执行
            ps.executeBatch();
            // 提交事务
            conn.commit();
            long end = System.currentTimeMillis();
            System.out.println("添加10000条数据共耗费了" + (end - begin) + "毫秒");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("未找到驱动");
            // 回滚事务
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库失败");
            // 回滚事务
            conn.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器内部异常");
            // 回滚事务
            conn.rollback();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("释放连接出错");
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
